package com.yyfax.ph.personal.bindapply;

import android.text.TextUtils;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * 绑定申请presenter
 */
public class BindApplyPresenter extends Presenter<BindApplyModel, BindApplyPresenter.View> {
	private static final String TAG = BindApplyPresenter.class.getSimpleName();

	private boolean mStatus = true;//状态类型：true.下拉刷新  false.上拉加载
	private View mView;
	private String mType;

	public static class Generator implements PresenterFactory<BindApplyPresenter> {
		@Override
		public BindApplyPresenter create() {
			return new BindApplyPresenter();
		}
	}

	@Override
	public void attach(View view) {
		mView = view;
		mModel = new BindApplyModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public interface View extends IView {
		void refreshSuccess(List<BindApplyModel.ApplyListBean> list);

		void loadmoreSuccess(List<BindApplyModel.ApplyListBean> list);

		void getSubAccountFailure(String tips);

		void handleBindSuccess(String type);

		void handleBindFailure(String tips);

		void handleBindWarning(String tips);
	}

	//获取子账户绑定申请列表
	public void getApplyAccounts(int pageNo, int pageSize, boolean status) {
		mStatus = status;
		execute(new BindApplyModel.ApplyList(String.valueOf(pageNo), String.valueOf(pageSize)), BindApplyModel.MSG_GET_BIND_ACCOUNTS, false);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void applyListResult(BindApplyModel.ApplyListResult result) {
		CLog.i(TAG, result.toString());
		if (isResponseSuccess(result)) {
			if (mStatus)
				mView.refreshSuccess(result.getContent());
			else
				mView.loadmoreSuccess(result.getContent());
		} else
			mView.getSubAccountFailure(TextUtils.isEmpty(result.getMsg()) ? "获取数据失败" : result.getMsg());
	}

	//接受或者拒绝子账户绑定申请
	public void handleBind(String subUserId, String type) {
		mType = type;
		execute(new BindApplyModel.Handle(subUserId, type), BindApplyModel.MSG_HANDLE_BIND, true, mType, null);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void HandleBindResult(BindApplyModel.HandleResult result) {
		CLog.i(TAG, result.toString());
		if (isResponseSuccess(result)) {
			mView.handleBindSuccess((String) result.extraParam);
		} else if (isResponseWarning(result)) {
			mView.handleBindWarning(TextUtils.isEmpty(result.getMsg()) ? "页面已过期" : result.getMsg());
		} else {
			mView.handleBindFailure(TextUtils.isEmpty(result.getMsg()) ? "处理绑定申请失败" : result.getMsg());
		}
	}
}
